import { Component, OnInit } from '@angular/core';
import {InterviewerService} from '../../function/app.service';
import {NgExNzTable, NgExPaginationService} from 'ng-ex-tools';
import {InterviewerFormatterService} from '../../function/interviewer-formatter.service';
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {ShowResultComponent} from '../show-result/show-result.component';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import * as CryptoJS from 'crypto-js';

@Component({
  selector: 'app-register',
  templateUrl: './check-interviewer.component.html',
  styleUrls: ['./check-interviewer.component.scss']
})
export class CheckInterviewerComponent implements OnInit {
  form: FormGroup;
  error = '';
  public passwordVisible = false;
  pagination: NgExPaginationService;
  interviewerTable = new NgExNzTable({
    headers: this.formatter.formatter,
    showPageSizeChanger: true,
    pageSizeOptions: [15, 30, 50, 100],
    operations: [
      {
        text: '人格查看',
        click: (e) => {
          this.showResult(e.answer);
        }
      },
    ],
  });
  logined = false;
  constructor(private interviewerService: InterviewerService,
              fb: FormBuilder,
              private nzModal: NzModalService,
              private nzMessage: NzMessageService,
              private formatter: InterviewerFormatterService) {
    this.form = fb.group({
      user: ['admin', [Validators.required]],
      password: ['superadmin', Validators.required],
    });
    this.pagination = new NgExPaginationService(this.interviewerService);
  }

  ngOnInit() {
  }
  clearError() {
    this.error = '';
  }

  submit() {
    if (this.form.valid) {
      this.form.value.user = this.form.value.user.replace(/\s*/g, '');
      this.interviewerService.post(this.interviewerService.urls.login, this.form.value).subscribe(() => {
        this.logined = true;
      }, (error: any) => {
        this.nzMessage.error(error.error);
      });
    }
  }
  showResult(result: any) {
    const resultModal = this.nzModal.create(
      {
        nzTitle: '人格分析',
        nzContent: ShowResultComponent,
        nzWidth: 500,
        nzComponentParams: {
          data: result,
        },
        nzKeyboard: false,
        nzMaskClosable: false,
      }
    );
  }
}
